package com.amikomgamedev.labirinhantu.state;

import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;

import android.view.KeyEvent;

import com.amikomgamedev.labirinhantu.Rule;
import com.amikomgamedev.labirinhantu.Server_Data;
import com.amikomgamedev.labirinhantu.Utils;
import com.amikomgamedev.labirinhantu.entity.Entity_Camera;
import com.amikomgamedev.labirinhantu.entity.Entity_Fire;
import com.amikomgamedev.labirinhantu.entity.Entity_Ghost;
import com.amikomgamedev.labirinhantu.entity.Entity_Mc;
import com.amikomgamedev.labirinhantu.entity.Entity_Timer;
import com.amikomgamedev.labirinhantu.grafik.Grafik_Anim;

public class State_Game_Play
	extends
	    State_Game_Pause
	implements
	    Grafik_Anim,
	    Rule
{
    // private Sound_Manager m_Sound_Manager

    private Entity_Mc	      m_Entity_Mc;
    private Entity_Fire	    m_Entity_Fire;
    private Entity_Camera	  m_Entity_Camera;
    private Entity_Ghost	   m_Entity_Ghost;
    private Entity_Ghost	   m_Entity_Ghost_Invisible;
    private Entity_Timer	   m_Entity_Timer;

    private boolean		usePelampung  = false;
    private boolean		isSwimming    = false;
    private boolean		isNotSwimming = true;
    // private ArrayList<Boolean> lstIsSwimming = new ArrayList<Boolean>();
    
    private boolean		isShowGhost   = false;

//    private DigitalOnScreenControl control;

    protected void create_Game_Play()
    {
	Server_Data.resetRules();

//	int x = m_DB.getTulang(Server_Data.getStage(), Server_Data.getMap());
	
	grafik.loadGamePlayBg();
	grafik.attachGamePlayBg();
	grafik.attachGamePlayTaskbarBg();
	grafik.attachGamePlayBtn();
	grafik.attachObjectFromTMX();
	grafik.attachObjInfoFrmTmx();
	grafik.attachIconSocialGame();
	teks.attachClue(grafik.lst_Game_Over_Bg.get(0));
	
	m_Hud.setPosition(0, 0);
	
	m_Entity_Mc = new Entity_Mc(
		this,
		m_Scene,
		grafik.spr_Game_Play_Mc,
		grafik.lst_Game_Play_Wall,
		grafik.spr_Game_Play_Btn[0]);

	m_Entity_Camera = new Entity_Camera(
		m_Camera,
		grafik.tmx_Layer_Game_Play,
		grafik.spr_Game_Play_Mc[0]);

	m_Entity_Ghost = new Entity_Ghost(
		grafik.lst_Game_Play_Ghost,
		grafik.lst_Game_Play_Move_Ghost);

	m_Entity_Ghost_Invisible = new Entity_Ghost(
		grafik.lst_Game_Play_Ghost_Invisible,
		grafik.lst_Game_Play_Move_Ghost);

	m_Entity_Fire = new Entity_Fire(
		grafik.lst_Game_Play_Fire,
		grafik.lst_rec_Game_Play_Fire);

	m_Entity_Timer = new Entity_Timer();

	initRule();
    }

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
    {
	if (State_Menu_Current == STATE_GAME_PLAY)
	{
	    m_ClickDetector.onTouchEvent(pSceneTouchEvent);

	    if (pSceneTouchEvent.isActionDown())
	    {
//		if (pSceneTouchEvent.getX() < RES_SPLIT_MAIN_WIDTH)
		    attachGamePlayControl(
			    (grafik.tmx_Layer_Game_Play.getX() - m_Camera.getXMin() + pSceneTouchEvent.getX()) * m_Camera.getZoomFactor(),
			    (grafik.tmx_Layer_Game_Play.getY() - m_Camera
				    .getYMin() + pSceneTouchEvent.getY()) * m_Camera
				    .getZoomFactor());
		// grafik.dig_Game_Play_Control.setVisible(true);
	    }

	    if (pSceneTouchEvent.isActionUp())
		detachGamePlayControl();
	    // grafik.dig_Game_Play_Control.setVisible(false);
	    //
	    // grafik.dig_Game_Play_Control.setPosition(
	    // pSceneTouchEvent.getX() -
	    // grafik.dig_Game_Play_Control.getControlBase().getWidth() / 2,
	    // pSceneTouchEvent.getY() -
	    // grafik.dig_Game_Play_Control.getControlBase().getHeight() / 2);

	    if (pSceneTouchEvent.isActionUp())
		m_Entity_Mc.switchState(m_Entity_Mc.IDLE);
	}

	return super.onSceneTouchEvent(pScene, pSceneTouchEvent);
    }

    @Override
    public boolean onAreaTouched(
	    TouchEvent pSceneTouchEvent,
	    ITouchArea pTouchArea,
	    float pTouchAreaLocalX,
	    float pTouchAreaLocalY)
    {
	if (State_Menu_Current == STATE_GAME_PLAY)
	{
	    for (int i = 0; i < grafik.lst_Game_Play_Btn.size(); i++)
		if (pTouchArea.equals(grafik.lst_Game_Play_Btn.get(i)) && (pSceneTouchEvent
			.isActionMove()))
		{
		    m_Entity_Mc.switchState(i);
		}

	    if (pTouchArea.equals(grafik.spr_Game_Play_Btn[1]))
		switchState(STATE_GAME_PAUSE);

	    if (pTouchArea.equals(grafik.spr_Game_Play_TaskBar_Btn))
		if (pSceneTouchEvent.isActionDown())
		    if (m_Entity_Camera.isZoomIn())
			m_Entity_Camera.zoomOut();
		    else
			m_Entity_Camera.zoomIn();
	}

	return super.onAreaTouched(
		pSceneTouchEvent,
		pTouchArea,
		pTouchAreaLocalX,
		pTouchAreaLocalY);
    }

    @Override
    public void onUpdate(float pSecondsElapsed)
    {

	if (State_Menu_Current == STATE_GAME_PLAY)
	{
	    initColides();
	    switchAnimateTaskbar();
	    updateRule();
	    checkRule();

	    m_Entity_Mc.onUpdate(pSecondsElapsed);
	    m_Entity_Fire.onUpdate(pSecondsElapsed);
	    m_Entity_Camera.onUpdate();
	    m_Entity_Ghost.checkMove();
	    m_Entity_Ghost_Invisible.checkMove();
	    m_Entity_Timer.onUpdate(pSecondsElapsed);
	}

	super.onUpdate(pSecondsElapsed);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
	if (State_Menu_Current == STATE_GAME_PLAY)
	{
	    if (keyCode == KeyEvent.KEYCODE_BACK)
	    {
		Utils.TRACE("INI BACK PLAY");
		// grafik.spr_Game_Pause_Bg.setVisible(false);
		State_Menu_Current = STATE_GAME_PAUSE;
	    }
	}

	return super.onKeyUp(keyCode, event);
    }

    private void initColides()
    {
	for (int i = 0; i < grafik.lst_Game_Play_Float.size(); i++)
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Float.get(i)))
	    {
		grafik.detachGamePlayFloat(i);
		usePelampung = true;

		grafik.spr_Game_Play_TaskBar_Mc.animate(
			GAME_PLAY_TASKBAR_MC_DUR[0],
			GAME_PLAY_TASKBAR_MC_FRAME[0],
			false);
	    }

	for (int i = 0; i < grafik.lst_Game_Play_Collection.size(); i++)
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Collection.get(i)))
	    {
		sound.playGameSfx(SFX_GAME_TULANG);

		grafik.detachGamePlayCollection(i);
		Server_Data.setKoin(Server_Data.getKoin() - 1);

		if (Server_Data.getKoin() == 0)
		    grafik.detachGamePlayRuleCoin();
	    }

	for (int i = 0; i < grafik.lst_Game_Play_Item_Right.size(); i++)
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Item_Right.get(i)))
	    {
		grafik.detachGamePlayItemRight(i);
		Server_Data.setRight(Server_Data.getRight() + 1);
	    }

	for (int i = 0; i < grafik.lst_Game_Play_Item_Left.size(); i++)
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Item_Left.get(i)))
	    {
		grafik.detachGamePlayItemLeft(i);
		Server_Data.setLeft(Server_Data.getLeft() + 1);
	    }

	for (int i = 0; i < grafik.lst_Game_Play_Btn_Ghost.size(); i++)
	{
	    if (i == 0)
		isShowGhost	= false;
	    
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Btn_Ghost.get(i)))
	    {
		Utils.TRACE("VISIBLE GHOST");
		for (int j = 0; j < grafik.lst_Game_Play_Ghost_Invisible.size(); j++)
		    grafik.lst_Game_Play_Ghost_Invisible
			    .get(j)
			    .setVisible(true);

		isShowGhost = true;

		grafik.lst_Game_Play_Btn_Ghost.get(i).setCurrentTileIndex(1);
		// return;
	    }
	}

	if (!isShowGhost)
	{
	    for (int j = 0; j < grafik.lst_Game_Play_Ghost_Invisible.size(); j++)
		grafik.lst_Game_Play_Ghost_Invisible.get(j).setVisible(false);

	    for (int i = 0; i < grafik.lst_Game_Play_Btn_Ghost.size(); i++)
		grafik.lst_Game_Play_Btn_Ghost.get(i).setCurrentTileIndex(0);
	}

	for (int i = 0; i < grafik.lst_Game_Play_Btn_Mist.size(); i++)
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Btn_Mist.get(i)))
	    {
		Utils.TRACE("BTN MIST");

		for (int j = 0; j < grafik.lst_Game_Play_Mist.size(); j++)
		    grafik.lst_Game_Play_Mist.get(j).setVisible(false);
	    }

	for (int i = 0; i < grafik.lst_Game_Play_Mist.size(); i++)
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Mist.get(i)) && grafik.lst_Game_Play_Mist
		    .get(i)
		    .isVisible())
	    {
		m_Entity_Mc.respawn_Mc();
		return;
	    }

	for (int i = 0; i < grafik.lst_Game_Play_Water.size(); i++)
	{
	    if (i == 0)
		isNotSwimming = true;

	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Water.get(i)))
	    {
		if (usePelampung)
		{
		    if (isSwimming == false)
			isSwimming = true;

		    isNotSwimming = false;
		    // lstIsSwimming.add(true);

		    grafik.spr_Game_Play_Mc[0].setVisible(false);
		    grafik.spr_Game_Play_Mc[1].setVisible(true);
		}
		else
		{
		    if (NO_DEATH == true)
			return;

		    Utils.TRACE("GAME OVER AIR");
		    m_Entity_Mc.switchState(m_Entity_Mc.DIE_SINK);
		    switchState(STATE_GAME_OVER);
		    return;
		}
	    }
	}

	if (isNotSwimming)
	{
	    grafik.spr_Game_Play_Mc[0].setVisible(true);
	    grafik.spr_Game_Play_Mc[1].setVisible(false);

	    if (isSwimming)
	    {
		usePelampung = false;
		isSwimming = false;

		grafik.spr_Game_Play_TaskBar_Mc.animate(
			GAME_PLAY_TASKBAR_MC_DUR[1],
			GAME_PLAY_TASKBAR_MC_FRAME[1],
			true);
	    }

	}

	// if(grafik.lst_Game_Play_Water.size() == 0)
	// grafik.spr_Game_Play_Mc[1].setVisible(false);

	for (int i = 0; i < grafik.lst_rec_Game_Play_Fire.size(); i++)
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_rec_Game_Play_Fire.get(i)))
	    {
		if (NO_DEATH == true)
		    return;

		Utils.TRACE("GAME OVER API");
		m_Entity_Mc.switchState(m_Entity_Mc.DIE_ON_FIRE);
		switchState(STATE_GAME_OVER);
		return;
	    }

	for (int i = 0; i < grafik.lst_Game_Play_Ghost.size(); i++)
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Ghost.get(i)))
	    {
		if (NO_DEATH == true)
		    return;

		Utils.TRACE("GAME OVER GHOST");
		m_Entity_Mc.switchState(m_Entity_Mc.DIE_SEE_GHOST);
		switchState(STATE_GAME_OVER);
		return;
	    }

	for (int i = 0; i < grafik.lst_Game_Play_Ghost_Invisible.size(); i++)
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Ghost_Invisible.get(i)))
	    {
		if (NO_DEATH == true)
		    return;

		Utils.TRACE("GAME OVER GHOST INVIS");
		m_Entity_Mc.switchState(m_Entity_Mc.DIE_SEE_GHOST);
		switchState(STATE_GAME_OVER);
		return;
	    }

	for (int i = 0; i < grafik.lst_Game_Play_Finish.size(); i++)
	    if (grafik.spr_Game_Play_Mc[0]
		    .collidesWith(grafik.lst_Game_Play_Finish.get(i)))
	    {
		if (Server_Data.getKoin() > 0)
		{
		    if (NO_DEATH == true)
			return;

		    Utils.TRACE("GAME OVER FINISH");
		    switchState(STATE_GAME_OVER);
		    return;
		}
		else
		{
		    cekJmlTulang();
		    m_Entity_Mc.switchState(m_Entity_Mc.WIN);
		    switchState(STATE_GAME_WIN);
		    return;
		}
	    }
    }

    private void switchAnimateTaskbar()
    {
	if (m_Entity_Timer.getTime() > RULE_TIME[Server_Data.getStage()][Server_Data
		.getMap()][1] && grafik.spr_Game_Play_TaskBar_Mc
		.getCurrentTileIndex() < 4)
	{
	    if (!usePelampung)
		grafik.spr_Game_Play_TaskBar_Mc.animate(
			GAME_PLAY_TASKBAR_MC_DUR[1],
			GAME_PLAY_TASKBAR_MC_FRAME[2],
			true);
	}
	else if (m_Entity_Timer.getTime() > RULE_TIME[Server_Data.getStage()][Server_Data
		.getMap()][2] && grafik.spr_Game_Play_TaskBar_Mc
		.getCurrentTileIndex() < 7)
	{
	    if (!usePelampung)
		grafik.spr_Game_Play_TaskBar_Mc.animate(
			GAME_PLAY_TASKBAR_MC_DUR[1],
			GAME_PLAY_TASKBAR_MC_FRAME[3],
			true);
	}
    }

    private void cekJmlTulang()
    {
	if (m_Entity_Timer.getTime() > RULE_TIME[Server_Data.getStage()][Server_Data
		.getMap()][2])
	    Server_Data.setJmlTulang(1);
	else if (m_Entity_Timer.getTime() > RULE_TIME[Server_Data.getStage()][Server_Data
		.getMap()][1])
	    Server_Data.setJmlTulang(2);
	else
	    Server_Data.setJmlTulang(3);

//	if (Server_Data.getTulang(Server_Data.getStage(), Server_Data.getMap()) < Server_Data
//		.getJmlTulang())
//	    Server_Data.setTulang(
//		    Server_Data.getStage(),
//		    Server_Data.getMap(),
//		    Server_Data.getJmlTulang());

	if (m_DB.getTulang(Server_Data.getStage(), Server_Data.getMap()) < Server_Data
		.getJmlTulang())
	    m_DB.setTulang(
		    Server_Data.getStage(),
		    Server_Data.getMap(),
		    Server_Data.getJmlTulang());
    }

    private void initRule()
    {
	for (int i = 0; i < grafik.lst_Game_Play_Taskbar_Rule.size(); i++)
	{
	    if (grafik.lst_Game_Play_Taskbar_Rule.get(i).getUserData() != null)
	    {

		if (grafik.lst_Game_Play_Taskbar_Rule
			.get(i)
			.getUserData()
			.equals("Koin"))
		    teks.attachRule(grafik.lst_Game_Play_Taskbar_Rule.get(i));
		else if (grafik.lst_Game_Play_Taskbar_Rule
			.get(i)
			.getUserData()
			.equals("Right"))
		    teks.attachRule(grafik.lst_Game_Play_Taskbar_Rule.get(i));
		else if (grafik.lst_Game_Play_Taskbar_Rule
			.get(i)
			.getUserData()
			.equals("Left"))
		    teks.attachRule(grafik.lst_Game_Play_Taskbar_Rule.get(i));
		else if (grafik.lst_Game_Play_Taskbar_Rule
			.get(i)
			.getUserData()
			.equals("Back"))
		    teks.attachRule(grafik.lst_Game_Play_Taskbar_Rule.get(i));
	    }
	}
    }

    private void updateRule()
    {
	for (int i = 0; i < grafik.lst_Game_Play_Taskbar_Rule.size(); i++)
	{
	    // if (grafik.lst_Game_Play_Taskbar_Rule.size() != 0)
	    {
		if (grafik.lst_Game_Play_Taskbar_Rule.get(i).getUserData() != null)
		{
		    if (grafik.lst_Game_Play_Taskbar_Rule
			    .get(i)
			    .getUserData()
			    .equals("Koin"))
			teks.lst_Rule
				.get(i)
				.setText("" + Server_Data.getKoin());
		    if (grafik.lst_Game_Play_Taskbar_Rule
			    .get(i)
			    .getUserData()
			    .equals("Right"))
			teks.lst_Rule
				.get(i)
				.setText("" + Server_Data.getRight());
		    if (grafik.lst_Game_Play_Taskbar_Rule
			    .get(i)
			    .getUserData()
			    .equals("Left"))
			teks.lst_Rule
				.get(i)
				.setText("" + Server_Data.getLeft());
		    if (grafik.lst_Game_Play_Taskbar_Rule
			    .get(i)
			    .getUserData()
			    .equals("Back"))
			teks.lst_Rule
				.get(i)
				.setText("" + Server_Data.getBack());
		}
	    }
	}
    }

    private void checkRule()
    {
	if (NO_DEATH == true)
	    return;

	// if (Server_Data.getKoin() == -1)
	// {
	// switchState(STATE_GAME_OVER);
	// return;
	// }
	if (Server_Data.getRight() == -1)
	{
	    switchState(STATE_GAME_OVER);
	    return;
	}
	if (Server_Data.getLeft() == -1)
	{
	    switchState(STATE_GAME_OVER);
	    return;
	}
	if (Server_Data.getBack() == -1)
	{
	    switchState(STATE_GAME_OVER);
	    return;
	}
    }

    @Override
    public void onControlChange(
	    BaseOnScreenControl pBaseOnScreenControl,
	    float pValueX,
	    float pValueY)
    {
	if (pValueX > 0)
	    m_Entity_Mc.switchState(0);
	else if (pValueX < 0)
	    m_Entity_Mc.switchState(2);
	else if (pValueY > 0)
	    m_Entity_Mc.switchState(1);
	else if (pValueY < 0)
	    m_Entity_Mc.switchState(3);
	else
	    m_Entity_Mc.switchState(m_Entity_Mc.IDLE);

	super.onControlChange(pBaseOnScreenControl, pValueX, pValueY);
    }

    private void attachGamePlayControl(float p_X, float p_Y)
    {
	if (p_X <= grafik.spr_Game_Play_Btn[0].getWidth() / 2)
	    p_X = grafik.spr_Game_Play_Btn[0].getWidth() / 2;
	else if (p_X > RES_SPLIT_MAIN_WIDTH - grafik.spr_Game_Play_Btn[0]
		.getWidth() / 2)
	    p_X = RES_SPLIT_MAIN_WIDTH - grafik.spr_Game_Play_Btn[0].getWidth() / 2;

	if (p_Y <= grafik.spr_Game_Play_Btn[0].getHeight() / 2)
	    p_Y = grafik.spr_Game_Play_Btn[0].getHeight() / 2;
	else if (p_Y > RES_HEIGHT - grafik.spr_Game_Play_Btn[0].getHeight() / 2)
	    p_Y = RES_HEIGHT - grafik.spr_Game_Play_Btn[0].getHeight() / 2;

	grafik.spr_Game_Play_Btn[0].setPosition(
		p_X - grafik.spr_Game_Play_Btn[0].getWidth() / 2,
		p_Y - grafik.spr_Game_Play_Btn[0].getHeight() / 2);

	grafik.spr_Game_Play_Btn[0].setVisible(true);

	// control.getControlBase().setPosition(
	// f - control.getControlBase().getWidth() / 2,
	// g - control.getControlBase().getHeight() / 2);
	// control.getControlKnob().setPosition(
	// f - control.getControlKnob().getWidth() / 2,
	// g - control.getControlKnob().getHeight() / 2);
	//
	// control.setVisible(true);
    }

    private void detachGamePlayControl()
    {
	// control.setVisible(false);
	grafik.spr_Game_Play_Btn[0].setVisible(false);
    }
	
	@Override
	public synchronized void onPauseGame()
	{
	    if(State_Menu_Current == STATE_GAME_PLAY)
	    {
		sound.stopGameBgm(Server_Data.getStage());
		
		switchState(STATE_GAME_PAUSE);
	    }
	    
	    super.onPauseGame();
	}
}
